/* Copyright Thanh-Trung Pham, JAIST, 2012 */

package jp.ac.jaist.message;

/**
 *
 * @author Thanh-Trung Pham
 */
public class RMBaseException extends Exception {

    private String m_strExceptionID;
    private boolean m_isLogged;

    /**
     * Get the exception ID, in other way, error code.
     * @return the error code.
     */
    public String getExceptionID() {
        return m_strExceptionID;
    }

    /**
     * Check if the exception is logged.
     * @return true if the exception is logged, false otherwise.
     */
    public boolean isLogged() {
        return m_isLogged;
    }

    /**
     * Construct a RMBaseException with the resource message ID.
     * @param resourceMessageID the resource message ID.
     * @param isLogged true if the exception is logged, false otherwise.
     */
    public RMBaseException(String resourceMessageID, boolean isLogged) {
        super(RMMessageUtility.getRMResourceMessage(resourceMessageID));

        handleException(resourceMessageID, null, isLogged);
    }

    /**
     * Construct a RMBaseException with the resource message ID and a parameter.
     * @param resourceMessageID the resource message ID.
     * @param parameter the parameter.
     * @param isLogged true if the exception is logged, false otherwise.
     */
    public RMBaseException(String resourceMessageID, String parameter, boolean isLogged) {
        super(RMMessageUtility.getRMResourceMessage(resourceMessageID, parameter));

        handleException(resourceMessageID, null, isLogged);
    }

    /**
     * Construct a RMBaseException with the resource message ID, a parameter A and a parameter B.
     * @param resourceMessageID the resource message ID.
     * @param parameterA the parameter A.
     * @param parameterB the parameter B.
     * @param isLogged true if the exception is logged, false otherwise.
     */
    public RMBaseException(String resourceMessageID, String parameterA, String parameterB, boolean isLogged) {
        super(RMMessageUtility.getRMResourceMessage(resourceMessageID, parameterA, parameterB));

        handleException(resourceMessageID, null, isLogged);
    }

    /**
     * Construct a RMBaseException with the resource message ID, a parameter A, a parameter B and a parameter C.
     * @param resourceMessageID the resource message ID.
     * @param parameterA the parameter A.
     * @param parameterB the parameter B.
     * @param parameterC the parameter C.
     * @param isLogged true if the exception is logged, false otherwise.
     */
    public RMBaseException(String resourceMessageID, String parameterA, String parameterB, String parameterC, boolean isLogged) {
        super(RMMessageUtility.getRMResourceMessage(resourceMessageID, parameterA, parameterB, parameterC));

        handleException(resourceMessageID, null, isLogged);
    }

    /**
     * Construct a RMBaseException with the resource message ID and an exception.
     * @param resourceMessageID the resource message ID.
     * @param exception the exception.
     * @param isLogged true if the exception is logged, false otherwise.
     */
    public RMBaseException(String resourceMessageID, Exception exception, boolean isLogged) {
        super(RMMessageUtility.getRMResourceMessage(resourceMessageID, exception));

        handleException(resourceMessageID, null, isLogged);
    }

    /**
     * Construct a RMBaseException with the resource message ID, a parameter and an exception.
     * @param resourceMessageID the resource message ID.
     * @param parameter the parameter.
     * @param exception the exception.
     * @param isLogged true if the exception is logged, false otherwise.
     */
    public RMBaseException(String resourceMessageID, String parameter, Exception exception, boolean isLogged) {
        super(RMMessageUtility.getRMResourceMessage(resourceMessageID, parameter, exception));

        handleException(resourceMessageID, null, isLogged);
    }

    /**
     * Handle the exception by saving some information for later use.
     * @param resourceMessageID the resource message ID.
     * @param exception the exception.
     * @param isLogged true if the exception is logged, false otherwise.
     */
    private void handleException(String resourceMessageID, Exception exception, boolean isLogged) {
        m_strExceptionID = resourceMessageID;

        if (exception != null) {
            setStackTrace(exception.getStackTrace());
        }

        m_isLogged = isLogged;
    }
}
